(function(pageName){
  sugarcane.module[pageName] = function(){
    $('.j-pagescreen').addClass('fn-hide');
    var urlParams = sugarcane.getUrlParams(),
      docType = 3,
      //newsId = urlParams['newsId'],
      id = urlParams['id'];
    if ($('#j-screen-'+pageName+id).length) {
      var pageScreen = $('#j-screen-'+pageName+id);
      pageScreen.removeClass('fn-hide');
      var _scrollTop = pageScreen.data('scrolltop') || 0;
      setTimeout(function(){
        $(window).scrollTop(_scrollTop);
      }, 10);
      return false;
    }
    var pageScreen = $('<div class="j-pagescreen j-pagescreen-detail" id="j-screen-'+pageName+id+'"></div>');
    $('body').append(pageScreen);

    detailShow();

    // 获取新闻
    var detailData = {};
    function detailShow(){
      if (id) {
        sugarcane.ajaxApi({
          "url": "/news/briefNews",
          "data": {"id": id},
          "callback": function(res){
            if (res['code'] == 0) {
              detailData = res['data']; 
              if(res.data.docType){
                docType = res.data.docType;
              }
              // 内容
              if (detailData['content']) {
                var newscontentObj = newGrapes["newscontent"].init({"content":detailData['content']});
                // 处理编辑携带的字体大小
                // newscontentObj.find('*').css("font-size", "inherit");
                // 处理编辑携带的字体大小，按屏幕大小等比显示，还有行高
                newscontentObj.find('*').each(function(){
                  var _fontSize = this.style.fontSize,
                    _lineHeight = this.style.lineHeight;
                  if (_fontSize) {
                    _fontSize = _fontSize.toLowerCase();
                    if (_fontSize.indexOf('px') > -1) {
                      _fontSize = (_fontSize.replace('px', '') * pageScreen.width() / 375) + 'px';
                      $(this).css('font-size', _fontSize);
                    }
                  }
                  if (_lineHeight) {
                    _lineHeight = _lineHeight.toLowerCase();
                    if (_lineHeight.indexOf('px') > -1) {
                      _lineHeight = (_lineHeight.replace('px', '') * pageScreen.width() / 375) + 'px';
                      $(this).css('line-height', _lineHeight);
                    }
                  }
                });

                // 设置字体大小
                pageScreen.addClass('app-fontsize-' + sugarcane['app']['fontSize']);

                pageScreen.append(newscontentObj);

              }
              // shareBanner();
              sugarcane['shareBanner']({
                'id': id,
                'docType': docType,
                'containObj': pageScreen 
              });
            } else {
              showerrorView();
              peach.hiddenAppbar()
              // alert(res['message']);
            }
          }
        });
      }
    }

    // function shareBanner(){
    //   if(sugarcane['app']['isShare'] && !pageScreen.find('.app-banner').length ){
    //     sugarcane.module.load('banner', function(listitemFn){
    //       var bannerObj = listitemFn({'id': id, 'docType': docType});
    //       pageScreen.append(bannerObj);
    //       var _height = $(bannerObj).css('height');
    //       pageScreen.css('margin-bottom',_height);
    //     });
    //   }
    // }
    
    function showerrorView(){
      var clientHeight = document.documentElement.clientHeight;
      pageScreen.css('position','relative').css('height',clientHeight+'px');
      var errorviewObj = $(`<div class="app-errorview">
          <img class="img" src="/static/img/isempty.png">
        <p class="error-tip">暂时没有数据</p>
      </div>`);
       pageScreen.append(errorviewObj);
    }
    
  }

  sugarcane.module['state'][pageName] = 'load';
  sugarcane.module.callback(pageName);
}('quickdetail'));
